package com.example.twopart;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private Button button;
    private ProgressBar progressBar;
    private TextView tvWendu;
    private TextView tWendu;
    private TextView tvShidu;
    private TextView tShidu;
    private TextView tvShengshi;
    private TextView tShengshi;
    private TextView tvFengsu;
    private TextView tFengsu;
    private EditText editTextTextPersonName4;
    private Button button2;
    private ImageView imageView;
    private TextView tvWeather;
    private TextView tWeather;
    private ImageView imgTianqiJpg;
    private TextView tTitle;

    private TextView tUpdatatime;
    private TextView txtCity;











    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.t_updatatime);

        tvWendu = (TextView) findViewById(R.id.tv_wendu);
        tWendu = (TextView) findViewById(R.id.t_wendu);
        tvShidu = (TextView) findViewById(R.id.tv_shidu);
        tShidu = (TextView) findViewById(R.id.t_shidu);
        tvShengshi = (TextView) findViewById(R.id.tv_shengshi);
        tShengshi = (TextView) findViewById(R.id.t_shengshi);
        tvFengsu = (TextView) findViewById(R.id.tv_fengsu);
        tFengsu = (TextView) findViewById(R.id.t_fengsu);
        editTextTextPersonName4 = (EditText) findViewById(R.id.editTextTextPersonName4);
        tvWeather = (TextView) findViewById(R.id.tv_weather);
        tWeather = (TextView) findViewById(R.id.t_weather);
        button2 = (Button) findViewById(R.id.button2);
        imgTianqiJpg = (ImageView) findViewById(R.id.img_tianqi_jpg);
        tUpdatatime = (TextView) findViewById(R.id.t_updatatime);
        txtCity = (TextView) findViewById(R.id.txt_city);

        tTitle = (TextView) findViewById(R.id.t_title);

    }


    public void qingqiu(View view) {


        try{
            Weather weather=new Weather();
            weather.execute(new URL("https://api.help.bj.cn/apis/weather/?id="+editTextTextPersonName4.getText().toString()));
       }
        catch (MalformedURLException e){
            e.printStackTrace();
        }

    }

    class Weather extends AsyncTask<URL,Integer,String>{
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            WeatherBean weatherBean=JSON.parseObject(s,WeatherBean.class);

            txtCity.setText(weatherBean.getCity());
            tUpdatatime.setText(weatherBean.getUptime());
            tTitle.setText(weatherBean.getToday());
            tWendu.setText(weatherBean.getTemp()+"   ℃");
            tShidu.setText(weatherBean.getHumidity());
            tShengshi.setText(weatherBean.getCity());
            tFengsu.setText(weatherBean.getWdspd());
            tWeather.setText(weatherBean.getWeather());
            //将天气文字转换为图片
            if(weatherBean.getWeather()!=null){
            if(weatherBean.getWeather().equals("晴")){
                Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                imgTianqiJpg.setBackgroundResource(R.drawable.qing);
            }
            if(weatherBean.getWeather().equals("多云")){
                Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                imgTianqiJpg.setBackgroundResource(R.drawable.duoyun);
            }
            if(weatherBean.getWeather().equals("阴")){
                Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                imgTianqiJpg.setBackgroundResource(R.drawable.ying);
            }
            //判断天气情况中是否含有该关键字
            if(weatherBean.getWeather().indexOf("雨")!=-1){
                   Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                   imgTianqiJpg.setBackgroundResource(R.drawable.yu);
               }
                if(weatherBean.getWeather().indexOf("雪")!=-1){
                    Log.d("MainActivity","输出天气"+weatherBean.getWeather().toString());
                    imgTianqiJpg.setBackgroundResource(R.drawable.xue);
                }
            }else {
                Log.d("MainActivity","未找到该天气");
                imgTianqiJpg.setBackgroundResource(R.drawable.yuanshi);
            }



        }

        @Override
        protected String doInBackground(URL... urls) {
            String s=HttpGetData(urls[0]);
            Log.d("MainActivity","获得天气数据"+s);

            return s;
        }
    }

    //这个方法将执行
    public String HttpGetData(URL url){
        String s=null;
        try{  //1。网络连接创建
            HttpURLConnection httpURLConnection=  (HttpURLConnection) url.openConnection();
            httpURLConnection.setConnectTimeout(10000);
            httpURLConnection.setReadTimeout(10000);
            httpURLConnection.connect();
            //2.发起连接
            if(httpURLConnection.getResponseCode()==200){
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                s =bufferedReader.readLine();
            }}
        catch (Exception e){
            e.printStackTrace();
        }
        return s;
    }



}
